﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NfsCommon.Export
{
    public sealed class CommonFunc
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public CommonFunc()
        {
        }

        /// <summary>
        /// 密码的Key值，随机生成的。


        /// </summary>
        public readonly string PASSWORDKEY = "asdLKU@#456";

        /// <summary>
        /// 用指定的Key加密明文
        /// </summary>
        /// <param name="sPlain">明文</param>
        /// <param name="sKey">密钥</param>
        /// <returns>返回密文</returns>
        public string Encrypt(string sPlain, string sKey)
        {
            if (String.IsNullOrEmpty(sPlain))
            {
                return sPlain;
            }
            //1、得到明文的UTF8的Bytes
            byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(sPlain);
            //2、声明密文的Bytes
            byte[] encryptBytes = new byte[plainBytes.Length];
            //3、得到密钥的UTF8的Bytes
            byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(sKey);
            //4、声明密钥的索引
            int nKey = 0;
            for (int i = 0; i < plainBytes.Length; i++)
            {
                //5、将明文与密钥的对应的Key异或运算
                int tmp = plainBytes[i] ^ keyBytes[nKey];
                //6、将异或的结果存入密文Bytes数组里


                encryptBytes[i] = (byte)tmp;
                //7、密钥的索引器加取余
                nKey = (nKey + 1) % keyBytes.Length;
            }
            //8、将密钥的Bytes转换成Base64码


            string sEncrypt = Convert.ToBase64String(encryptBytes);
            //9、返回密文


            return sEncrypt;
        }

        /// <summary>
        /// 用指定的Key去解密密文

        /// </summary>
        /// <param name="sEncrypt">密文</param>
        /// <param name="sKey">密钥</param>
        /// <returns>返回明文</returns>
        /// <remarks>
        /// 这里对密钥有没有修改没有判断
        /// </remarks>
        public string Decrypt(string sEncrypt, string sKey)
        {
            if (String.IsNullOrEmpty(sEncrypt))
            {
                return sEncrypt;
            }
            //1、从密文的Base64码得到Bytes
            byte[] encryptBytes = Convert.FromBase64String(sEncrypt);
            //2、声明明文的数组空间
            byte[] plainBytes = new byte[encryptBytes.Length];
            //3、得到Key的Bytes
            byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(sKey);
            //4、声明密钥的索引器


            int nKey = 0;
            for (int i = 0; i < encryptBytes.Length; i++)
            {
                //5、将密文指定Byte与密钥指定Byte异或运算
                int tmp = encryptBytes[i] ^ keyBytes[nKey];
                //6、将运算结果存入明文数组
                plainBytes[i] = (byte)tmp;
                //7、密钥索引器加取余


                nKey = (nKey + 1) % keyBytes.Length;
            }
            //8、得到明文


            string sPlain = System.Text.Encoding.UTF8.GetString(plainBytes);
            //9、返回明文


            return sPlain;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public string ToDateTimeFormat(string data, string format)
        {
            if (string.IsNullOrEmpty(data))
                return string.Empty;
            DateTime date = DateTime.Parse(data);
            return date.ToString(format);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ToDateTimeFormat(string data)
        {
            return ToDateTimeFormat(data, "yyyy/MM/dd HH:mm:ss");
        }

        /// <summary>
        /// 根据指定的字符串获得该字符串中前字母
        /// ABCD111 得到ABCD
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string GutStr(string str)
        {
            int i = 0;
            foreach (char c in str.ToCharArray())
            {
                int a = 0;
                try
                {
                    a = int.Parse(c.ToString());
                    break;
                }
                catch (Exception)
                { }
                i++;
            }
            return str.Substring(0, i);
        }

        /// <summary>
        /// 统计流量
        /// </summary>
        /// <param name="count">计数</param>
        /// <param name="path">文件路径</param>
        public static void SetWebCount(int count, string path)
        {
            string fileName = path;
            //如果存在文件
            if (!File.Exists(fileName))
            {
                FileStream fs = File.Create(fileName);
                fs.Close();
                fs.Dispose();
            }
            StreamWriter sw = new StreamWriter(fileName, false, System.Text.Encoding.Default);
            sw.Write(count.ToString());
            sw.Close();
            sw.Dispose();
        }

        /// <summary>
        /// 流量数
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static int GetWebCount(string path)
        {
            string fileName = path;
            //如果存在文件
            if (File.Exists(fileName))
            {
                StreamReader sr = new StreamReader(fileName);
                string count = sr.ReadLine();
                sr.Close();
                sr.Dispose();
                return int.Parse(count);
            }
            return 0;

        }
    }
}
